
package com.neusoft.neuiotms.dcom.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.ShiftConverter;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementResultDTO;
import com.neusoft.neuiotms.dcom.dto.ShiftDTO;
import com.neusoft.neuiotms.dcom.model.Shift;
import com.neusoft.neuiotms.dcom.model.enums.ShiftType;
import com.neusoft.neuiotms.dcom.repository.ShiftRepository;
import com.neusoft.neuiotms.dcom.service.DutyArrangementResultService;
import com.neusoft.neuiotms.dcom.service.ShiftService;

@Service
public class ShiftServiceImpl implements ShiftService {

    @Autowired
    private ShiftRepository shiftRepository;
    @Autowired
    private ShiftConverter shiftConverter;
    @Autowired
    private DutyArrangementResultService dutyArrangementResultService;

    @Override
    public List<ShiftDTO> index(final Searchable searchable) {
        final List<Shift> result = this.shiftRepository.index(searchable);
        return this.shiftConverter.toListDTO(result);
    }

    @Override
    public Page<ShiftDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<Shift> result = this.shiftRepository.search(pageable, searchable);
        return this.shiftConverter.toPageDTO(result);
    }

    @Override
    public ShiftDTO create(final ShiftDTO dto) {
        if (dto.getSourceUserCode().equals(dto.getTargetUserCode())) {
            throw new UnitedException("换班人不能指定为自己");
        }
        // 判断换班时间的那个时间段内是否有班
        if (dto.getType().equals(ShiftType.shift)) {
            final Searchable searchable = new Searchable();
            searchable.put("userName", dto.getTargetUserCode());
            searchable.put("afterStartTime", dto.getStartTime().getTime());
            searchable.put("beforeEndTime", dto.getStartTime().getTime());
            final List<DutyArrangementResultDTO> checkList = this.dutyArrangementResultService.index(searchable);

            if (CollectionUtils.isNotEmpty(checkList)) {
                throw new UnitedException("换班人在这个时间段内有班");
            }
        }

        final Shift model = this.shiftConverter.toModel(dto);
        this.shiftRepository.save(model);
        return this.shiftConverter.toDTO(model);
    }

    @Override
    public ShiftDTO update(final Long id, final ShiftDTO dto) {

        if (dto.getSourceUserCode().equals(dto.getTargetUserCode())) {
            throw new UnitedException("换班人不能指定为自己");
        }

        // 判断换班时间的那个时间段内是否有班
        if (dto.getType().equals(ShiftType.shift)) {
            final Searchable searchable = new Searchable();
            searchable.put("userName", dto.getTargetUserCode());
            searchable.put("afterStartTime", dto.getStartTime().getTime());
            searchable.put("beforeEndTime", dto.getStartTime().getTime());
            final List<DutyArrangementResultDTO> checkList = this.dutyArrangementResultService.index(searchable);

            if (CollectionUtils.isNotEmpty(checkList)) {
                throw new UnitedException("换班人在这个时间段内有班");
            }
        }

        Shift model;
        try {
            model = this.shiftRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.shiftConverter.toModel(dto);
        this.shiftRepository.save(model);
        return this.shiftConverter.toDTO(model);
    }

    @Override
    public ShiftDTO details(final Long id) {

        Shift model;
        try {
            model = this.shiftRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.shiftConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        this.shiftRepository.deleteById(id);
    }

    @Override
    public ShiftDTO updateStatus(final Long id, final boolean status) {

        Shift model;
        try {
            model = this.shiftRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        model.setStatus(status);
        this.shiftRepository.save(model);
        return this.shiftConverter.toDTO(model);
    }
}
